function submitLogin()
{
	$.ajax({
		url: siteUrl + 'login/check_login',
		type: 'post',
		data: $('#login').serialize(),
		dataType: 'json',
		beforeSend: function() {
			if ($('#user_no').val() == '' || $('#password').val() == '')
			{
				$('#err').children('.msg').html('Password is required.');
				$('#err').fadeIn('normal');
				return false;
			}
		},
		success: function(response) {

			if (response == true)
			{
				window.location = siteUrl + 'user';
			}
			else
			{
				$('#err').children('.msg').html('Invalid username and/or password.');
				$('#err').fadeIn('normal');
			}
		}
	});
}



function forgotPasswordRules()
{


	$('#forgot_password').validate({
		debug: true,
		rules: {
                        user_no:'required',
			email: {
				required: true,
				email: true
                                
			}

		},
		messages: {
			email: {
				required: $.Msgs('required'),
				email: $.Msgs('validEmail'),
			}



		},
		onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();//.remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
        submitHandler: function(form) {
            checkDetails();
        }
    });
}


//will verify if username and email is a match in forgotpassword.php view
//will send reseted password to user's email
//refer to admin.php controller for the function

function checkDetails(){
    $.ajax({
		url: siteUrl + 'admin/check_details',
		type: 'post',
		data: $('#forgot_password').serialize(),
		dataType: 'json',

              success: function(response) {
			if (response != true)
			{
                            $('#err').removeClass('hidden');
                            $('#err').children('.msg').html('Invalid username and/or email.');
                            $('#err').fadeIn('normal');
                            $('#success').addClass('hidden');
			}
                        else{
                            $('#success').removeClass('hidden');
                            $('#success').children('.msg').html('New password successfully sent to email');
                            $('#success').fadeIn('normal');
                            $('#err').addClass('hidden');
                        }
		}
	});

}

function changePasswordRules()
{
    
    	
    $.validator.addMethod('diffPassword', function(value) {
            valid = true;

            if ($('#new_password').val() != $('#confirm_password').val()){
                valid = false;
            }


            return valid;
    }, 'Password did not match');


    $('#change_password').validate({
        debug: true,
        rules: {

            old_password:{
                required: true
                ,remote: {
                    url: siteUrl + 'admin/check_password',
                    type: 'get',
                    data: {
                        old_password: function() {
                            return $('#old_password').val();
                        }
                    }
                }

            }

            ,new_password:{
                required: true
            }
            ,confirm_password:{
                required: true,
                diffPassword: true
            }



        },
        messages: {
             old_password:{
                remote:$.Msgs('invalidPassword')
            }


        },
        onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();//.remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
        submitHandler: function(form) {
            changePassword();
        }
    });

}

function changePassword(){

    $.ajax({
        url: siteUrl + 'admin/change_password/',
        type: 'post',
        data: $('#change_password').serialize(),
        dataType: 'json',
        success: function(response) {
            if (response == true)
            {

                $('#div_msg').children('.msg').html('Change password successful.');
                $('#div_msg').fadeIn('slow');

                $('#div_msg').delay(1000).fadeOut('slow');
            }

        }
    });


}



function registerRules(){
	$('#register_form').validate({
		rules: {
			username: {
				required: true,
				remote: {
                    url: siteUrl + 'admin/username_unique',
                    type: 'get',
                    data: {
                        field: 'username',

                    }
                }
			},
			passkey: {
				required: true,
				remote: {
                    url: siteUrl + 'admin/passkey',
                    type: 'get',
                    data: {
                        field: 'passkey',

                    }
                }
			},
			email: {
				
				required: true,
				email: true
				
			},
			password: 'required'

			
		},
		messages: {
			email: {
				required: 'Email is required',
				email: 'Enter a valid email address'
				
			},
			username: {
				required: 'Username is required',
				remote: 'Username already exists'
			},
			passkey: {
				required: 'Passkey is required',
				remote: 'Passkey does not exist'
			},
			password: 'Password is required'

			
		},
        onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
		submitHandler: function(form) {
			submitRegister();
		}
	});


}

function submitRegister() {
	$.ajax({
		url: siteUrl + 'admin/register',
		type: 'post',
		data: $('#register_form').serialize(),
		dataType: 'json',
		success: function(response) {

				//window.location = siteUrl + 'admin';
				alert('success');

		}
	});


}

